//
// Created by sjjmw on 3/24/2022.
//
#include "iostream"
#include "vector"
#include "list"
#include "algorithm"
#include "string"
#include "unordered_map"
#include "cmath"
#include<iomanip>
#include "string"
#include "map"
#include "list"
struct Sol{
std::vector<std::string> dcs;
std::vector<char> selected;
//    int len=1;
int maxlen=0;
    std::string append="";
    int same_part_len(int i1,int i2){
        auto &str1=dcs[i1];
        auto &str2=dcs[i2];
        std::string sub;
        bool found=false;
        for(int i=str1.size()-1;i>=0;i--){
            sub=str1.substr(i,str1.size()-i);
            if(str2.find(sub)==0){
                found= true;
                break;
            }
        }
        if(found){
            if(sub.size()!=str1.size()&&sub.size()!=str2.size()){
                return sub.size();
            }
        }
        return 0;
    }
    void dfs(
//            std::string ap,
             int len,int cnt,int last_i){
        if(len> maxlen){
            maxlen=len;
//            append=ap;
        }
//        maxlen= std::max(len,maxlen);
        for(int i=0;i<selected.size();i++){
            if(selected[i]<2){
                int same_len= same_part_len(last_i,i);
                if(same_len>0){
                    selected[i]++;
                    dfs(//ap.substr(0,ap.size()-same_len)
                              //  .append(dcs[i]),
                        len-same_len+dcs[i].size(),cnt+1,i);
                    selected[i]--;
                }
            }
        }
    }
    void handle(int n
            ){
        dcs.reserve(n);
        selected.resize(n);
        for(int i=0;i<n;i++){
            std::string str;
            std::cin>>str;
            dcs.emplace_back(str);
        }
        std::string a;std::cin>>a;
        for (int i = 0; i < dcs.size(); ++i) {
            auto&dc=dcs[i];
            if(dc[0]==a[0]){
                selected[i]++;
                dfs(
//                        dc,
                        dc.size(),1,i);
                selected[i]--;
            }
        }

        ;
        printf("%d\n",maxlen);
    }
};
int main(){
//    int a,b,c;
    float a,b,c,d;
    int e;
    while(std::cin>>a){
        Sol().handle(a);
    }

    return 0;
}